// ==============================================================================================
// RibbonTab.cs
//
// Created by: NI, 2007.03.27
// Revised by: NI, 2007.11.23
// ==============================================================================================
using System.Collections.Generic;
using System.Linq;
using RibbonExplorer.Entities;
using RibbonExplorer.Storage;

namespace RibbonExplorer.Entities
{
  // ==============================================================================================
  /// <summary>
  /// This class represents a tab of an Office application ribbon.
  /// </summary>
  // ==============================================================================================
  public class RibbonTab : ItemBase
  {
    #region Private fields

    private Ribbon _ParentRibbon;
    private List<Group> _Groups = new List<Group>();

    #endregion

    #region Lifecycle methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Creates a tab instance from the specified ID and name.
    /// </summary>
    /// <param name="id">Tab ID</param>
    /// <param name="parent">Parent ribbon</param>
    // --------------------------------------------------------------------------------------------
    public RibbonTab(string id, Ribbon parent)
      : base(id)
    {
      _ParentRibbon = parent;
    }

    #endregion

    #region Public properties

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the parent ribbon of this tab.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public Ribbon ParentRibbon
    {
      get { return _ParentRibbon; }
    }

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Gets the dictionary of groups belonging to this tab.
    /// </summary>
    // --------------------------------------------------------------------------------------------
    public List<Group> Groups
    {
      get { return _Groups; }
    }

    #endregion

    #region Overridden methods

    // --------------------------------------------------------------------------------------------
    /// <summary>
    /// Loads the groups belonging to this ribbon
    /// </summary>
    // --------------------------------------------------------------------------------------------
    protected override void OnLoadChildren()
    {
      // --- Try to obtain the ribbon table
      RibbonTable ribbonTable;
      if (RibbonTableCache.TryGetValue(_ParentRibbon.Id, out ribbonTable))
      {
        var groups = from control in ribbonTable.ControlList
                     where control.Type == ControlType.Group &&
                           control.Tab == Id
                     orderby control.Ordering
                     select control;

        foreach (RibbonTableItem item in groups)
        {
          Group newGroup = new Group(item.ControlId, this);
          _Groups.Add(newGroup);
        }
      }
    }

    #endregion
  }
}